﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using JasRibbonApplicatie.Classes;
using System.Timers;
using System.Windows.Threading;

namespace JasRibbonApplicatie.UserControls
{
    /// <summary>
    /// Interaction logic for MailItemControl.xaml
    /// </summary>
    public partial class MailItemControl : UserControl
    {
        private Timer t = new Timer(5000);
        public MailItemControl(Email email)
        {
            InitializeComponent();
            t.Elapsed += new ElapsedEventHandler(t_Elapsed);
            Email = email;
        }

        

        private Email _email;

        public Email Email
        {
            get { return _email; }
            set
            {
                _email = value;
                DateSent = DateTime.Parse(_email.date_sent_stamp.ToString());
                Subject = _email.subject;
                From = new EmailAdres(_email.from);
                Read = _email.seen;
                Flagged = _email.flagged;
            }
        }
        
        private DateTime DateSent
        {
            get
            {
                return DateTime.Parse(_email.date_sent_stamp.ToString());
            }
            set
            {
                if (DateTime.Now.Date == value.Date)
                {
                    dateTextBlock.Text = value.ToString("H:mm");
                }
                else
                {
                    dateTextBlock.Text = value.ToString("dd/MM/yyyy H:mm");
                }
                Email.date_sent_stamp = value;
            }
        }
        private string Subject
        {
            get
            {
                return Email.subject;
            }
            set
            {
                Email.subject = value;
                subjectTextBlock.Text = Email.subject;
            }
        }
        private bool Read
        {
            get
            {
                return Email.seen;
            }
            set
            {
                Email.seen = value;

                if (value)
                {
                    Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new DispatcherOperationCallback(delegate
                    {
                        readImage.Source = new ImageSourceConverter().ConvertFromString("pack://application:,,,/JasRibbonApplicatie;component/Images/mail/orig/Mail.Read.png") as ImageSource;
                        subjectTextBlock.FontWeight = FontWeights.Normal;
                        return null;
                    }), null);
                    
                }
                else
                {
                    Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new DispatcherOperationCallback(delegate
                    {
                        readImage.Source = new ImageSourceConverter().ConvertFromString("pack://application:,,,/JasRibbonApplicatie;component/Images/mail/orig/Mail.Unread.png") as ImageSource;
                        subjectTextBlock.FontWeight = FontWeights.Bold;
                        return null;
                    }), null);
                }
            }
        }
        private bool Flagged
        {
            get
            {
                return Email.flagged;
            }
            set
            {
                Email.flagged = value;
                if (value)
                {
                    Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new DispatcherOperationCallback(delegate
                    {
                        flagImage.Source = new ImageSourceConverter().ConvertFromString("pack://application:,,,/JasRibbonApplicatie;component/Images/mail/orig/fg1.png") as ImageSource;
                        return null;
                    }), null);
                }
                else
                {
                    Dispatcher.BeginInvoke(System.Windows.Threading.DispatcherPriority.Normal, new DispatcherOperationCallback(delegate
                    {
                        flagImage.Source = new ImageSourceConverter().ConvertFromString("pack://application:,,,/JasRibbonApplicatie;component/Images/mail/orig/fg0.png") as ImageSource;
                        return null;
                    }), null);
                }
            }
        }
        private EmailAdres From
        {
            get
            {
                return new EmailAdres(Email.from);
            }
            set
            {
                Email.from = value.ToJson();
                fromTextBlock.Text = value.ToString();
            }
        }

        private void flagImage_MouseUp(object sender, MouseButtonEventArgs e)
        {
            Flagged = !Flagged;
        }
        public void startSeing()
        {
            if (!Read)
            {
                t.Start();
            }
        }
        void t_Elapsed(object sender, ElapsedEventArgs e)
        {
            Read = true;
            t.Stop();
        }
        public void stopSeing()
        {
            if(t.Enabled) t.Stop();
        }
    }
}
